package com.daffodil.workable.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.annotation.PostConstruct;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

/**
 * -发送邮件告警工具类
 * @author yweijian
 * @date 2022年5月7日
 * @version 1.0
 * @description
 */
@Slf4j
@Component
public class SendEmailUtils {

    private static final String success = "OK";
    
    private static final String charset = "text/html;charset=UTF-8";
    
    @Value("${workable.mail.smtp.host:smtp.163.com}")
    private String smtphost;
    
    @Value("${workable.mail.smtp.port:25}")
    private Integer smtpport;
    
    @Value("${workable.mail.smtp.auth:true}")
    private String smtpauth;
    
    @Value("${workable.mail.protocol:smtp}")
    private String transportProtocol;
    
    @Value("${workable.mail.user:xxxxxx.163.com}")
    private String emailUser;
    
    @Value("${workable.mail.password:xxxxxxxx}")
    private String emailPassword;
    
    /** 邮件域名 */
    private static String host;
    /** 邮件端口 */
    private static Integer port;
    /** 是否认证 */
    private static String auth;
    /** 传输协议 */
    private static String protocol;
    /** 邮件账号 */
    private static String user;
    /** 邮件授权码 */
    private static String password;
    
    @PostConstruct
    public void init() {
        host = smtphost;
        port = smtpport;
        auth = smtpauth;
        protocol = transportProtocol;
        user = emailUser;
        password = emailPassword;
    }
    /**
     * -发送邮件
     * @param subject 邮件主题
     * @param content 邮件内容
     * @param emails 邮件接收者
     * @return
     */
    public static String sendEmail(String subject, String content, String[] emails) {
        
        Properties properties = new Properties();
        properties.put("mail.smtp.host", host);
        properties.put("mail.transport.protocol", protocol);
        properties.put("mail.smtp.auth", auth);
        Session session = Session.getInstance(properties);
        try (Transport transport = session.getTransport()){
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(user));
            List<InternetAddress> addresses = new ArrayList<InternetAddress>();
            for(String email : emails) {
                addresses.add(new InternetAddress(email));
            }
            message.addRecipients(Message.RecipientType.TO, addresses.toArray(new InternetAddress[emails.length]));
            message.setSubject(subject);
            message.setContent(content, charset);
            transport.connect(host, port, user, password);
            transport.sendMessage(message, message.getAllRecipients());
            return success;
        } catch (MessagingException e) {
            if(log.isWarnEnabled()) {
                log.warn(e.getMessage(), e);
            }
            return e.getMessage();
        }
    }
}
